package co.fitstart.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.support.Page;

import co.fitstart.admin.dao.ReceivablesDao;
import co.fitstart.admin.service.ReceivablesService;
import co.fitstart.entity.finance.Receivables;
import co.fitstart.entity.finance.ReceivablesExportRecord;

/**
 * 
 * @author dan
 *
 */
@Service("receivablesService")
public class ReceivablesServiceImpl implements ReceivablesService{
    
    @Dynamic
    private ReceivablesDao receivablesDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ReceivablesService#list(boolean, com.demo2do.core.support.Page)
     */
    public List<Receivables> list(boolean export, Page page) {
        
        List<Receivables> result = receivablesDao.list(export, page.getBeginIndex(), page.getMaxResultRecords());
        
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ReceivablesService#list(boolean)
     */
    public List<Receivables> list(boolean export) {
        return receivablesDao.list(export);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ReceivablesService#export(java.util.List, java.lang.Long)
     */
    public void export(List<Long> receivablesIds, Long operator) {
        // update receivables export 
        receivablesDao.export(receivablesIds);  
        // save sales order export record
        ReceivablesExportRecord record = new ReceivablesExportRecord(receivablesIds.size(), operator);
        genericDaoSupport.save(record);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ReceivablesService#listExportRecord(com.demo2do.core.support.Page)
     */
    public List<ReceivablesExportRecord> listExportRecord(Page page) {
        return receivablesDao.listExportRecord(page.getBeginIndex(), page.getMaxResultRecords());
    }

}
